<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>file/shell/smooth/resources/css/jquery.multiselect.css">
<script src="<?php echo base_url(); ?>file/shell/smooth/resources/scripts/jquery.multiselect.min.js"></script>

<div class="page-title">
  <h2>Create Sell In</h2>
</div>

<?php 
  $message = $this->session->flashdata('message');
  if ($message) : $div_class = $this->session->flashdata('div_class');
?>
  <div class="<?php $div_class; ?>">
    <?php echo $message; ?>
  </div>
<?php endif; ?>

<?php if(validation_errors()) : ?>
  <div class="box"><div id="box-messages">
    <div class="messages">
      <div id="message-success" class="message message-error">
        <div class="image">
          <img src="<?php echo base_url(); ?>file/shell/smooth/resources/images/icons/error.png" alt="Error" height="32" />
        </div>
        <div class="text"><?php echo validation_errors(); ?></div>
      <div class="dismiss"><a href="#message-success"></a></div>
      </div>
    </div>
  </div></div>
<?php endif; ?>

<div class="page page-trans">
  
<?php 
  // function backdate($param) {
  //   if ($param > 1) {
  //     echo '<select>';
  //     for ($i=0; $i < $param; $i++) { 
  //       echo '<option value="'.date("Y-m-d", strtotime("-".$i." day")).'">'.date("m/d/Y", strtotime("-".$i." day")).'</option>';
  //     }
  //     echo '</select>';
  //   }
  //   else 
  //     { echo '<input type="text" name="sell_in_date" id="date" placeholder="mm/dd/yyyy" value="'.date("m/d/Y").'">'; }
  // }

?>

<form action="<?php echo base_url();?>index.php/sell_out/newsell_out" method="post">
  <table class="trans-info">
    <tr>
      <td>Territory</td>
      <td><strong><?php echo $region.', '.$cluster; ?></strong></td>
    </tr>
    <tr>
      <td>Tanggal Sell Out</td>
      <td>
        <input type="text" name="sell_out_date" id="date" placeholder="yyyy-mm-dd" value="<?php echo date("Y-m-d")?>" readonly="readonly" required>
        <?php //backdate(2); ?>
      </td>
    </tr>
    <tr>
      <td>Nama Sales</td>
      <td>
        <select name="sales_id" id="sales-id">
          <option value=""></option>
            <?php foreach ($salesID as $key) : ?>
                <option value="<?php echo $key->user_group_id.'_'.$key->user_id; ?>"><?php echo $key->user_id.' | '.$key->user_group_caption.' | '.$key->user_name; ?></option>
            <?php endforeach; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Pilih Partner</td>
      <td>
        <select name="partner_id" id="partner-id">
          <option value=""></option>
          <!-- Appened partner list goes here -->
        </select>
      </td>
    </tr>
  </table>

  <div>
    <table class="tabel_transaksi" width="100%">
      <thead>
        <tr>
          <th>#</th>
          <th> </th>
          <th>ICCID</th>
          <th>MDN</th>
          <th>Group Item</th>
          <th>Nama Item</th>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <td colspan="7">REMARK:</td>
        </tr>
        <tr>
          <td colspan="7"><textarea name="remark" id="" style="resize: none; width:90%; font-family: inherit; font-size: 12px " rows="3"></textarea></td>
        </tr>
        <tr>
          <td colspan="7" class="align-center">
            <button type="submit" class="button" id="button-simpan" role="button" >Simpan</button>
            <button type="reset" class="button" id="button-reset" role="reset" >Batal</button>
          </td>
        </tr>
      </tfoot>
      <tbody class="daftar-item">
        <!-- di sini di append data agent/outlet -->
      </tbody>
    </table>
    
  </div>

</form>

</div>

<script type="text/javascript">
  $(document).ready(function() {
    $('button#button-simpan').attr('disabled','disabled');

    $('#sales-id').change(function(){
      if( isNaN($(this).val()) ) {
        $('button#button-simpan').removeAttr('disabled');
        var user = $(this).val().split('_');
        $('.daftar-item').empty();
        $('#partner-id').empty(); 
        $('#partner-id').append(new Option("Pilih Partner",""));
        if(user) {
          $.post(
            // "<?php echo base_url();?>index.php/sell/get_partners/"+sales_id,
            "<?php echo base_url();?>index.php/sell_out/getOutletList/",
            {user_group_id:user[0], sales_id:user[1]},
            function(data) {
              if(data.length != 0) {
                $.each(data, function(key, val){
                  $('#partner-id').append(new Option(val["channel_id"]+" | "+val["channel_name"], val["channel_id"]));
                });
              }
              else 
                { alert("Tidak ada Journey Cycle"); }
            }, "json"
          );
        }
      }
      else {
        $('button#button-simpan').attr('disabled','disabled');
        $('.daftar-item').empty();
        $('#partner-id').empty(); 
      }
    });

    // --------------------------------------------------------------------------------------    
    // Kode berikut ini untuk menampilkan list item, setelah pilih nama outlet
    $('#partner-id').change(function(){
      var iterasi = 1
      $('.daftar-item').empty();
      if($(this).val()) {
        $.post(
          "<?php echo base_url(); ?>index.php/sell_out/getItemList",
          {channel_id:$(this).val()},
          function(data) {
            var _d = jQuery.parseJSON(data);
            $.each(_d, function(k, v) {
              var item = "<tr>"+
                         "<td>"+iterasi+"</td>"+
                         "<td><input type='checkbox' name='iccid[]' id='iccid_"+iterasi+"' value='"+v['iccid']+"' /></td>"+
                         "<td><label for='iccid_"+iterasi+"'>"+v['iccid']+"</label></td>"+
                         "<td>"+v['mdn']+"</td>"+
                         "<td>"+v['item_group_name']+"</td>"+
                         "<td>"+v['item_name']+"</td>"+
                         "</tr>";
              iterasi++;
              $('.daftar-item').append(item);
            });
          }
        );
      }
    });

  });
</script>